#!/bin/bash

if [ $# -lt 2 ]; then
    echo "Usage: parallelize.sh command input1 [input2 [...]]"
    exit
fi

cmd=$1
shift 

num_cores=`grep 'processor' /proc/cpuinfo | sort -u | wc -l`
files_per_core=$(($#/$num_cores))

cmdfile=`mktemp`
filefile=`mktemp`
prefix=`mktemp`

for path in $@; do
    echo $path >> $filefile    
done

split -l$files_per_core $filefile $prefix

for path in `ls ${prefix}*`; do
    echo $cmd -o `basename $path`.hbook `cat $path` >> $cmdfile
done

cat $cmdfile | parallel -j$num_cores

